﻿using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Interactivity;
using NUnit.Framework;
using Chambills.PopUpBehavior;

namespace Chambills.PopUpBehaviour.Tests
{
    public partial class PopUpBehaviourTestCase
    {

         [TestFixture, RequiresSTA]
        public class PopUpBehaviourWhenVisibleScenario
        {
            private Grid host;
            private PopUpBehavior.PopUpBehaviour behaviour;
            private Grid addedGrid;

             [SetUp]
            public void SetUp()
            {
                behaviour = new PopUpBehavior.PopUpBehaviour { PopUp = new UIElement() };
                host = new Grid();
                host.ColumnDefinitions.Add(new ColumnDefinition());
                host.ColumnDefinitions.Add(new ColumnDefinition());
                host.ColumnDefinitions.Add(new ColumnDefinition());
                host.RowDefinitions.Add(new RowDefinition());
                host.RowDefinitions.Add(new RowDefinition());
                Interaction.GetBehaviors(host).Add(behaviour);
                behaviour.Visible = true;
               addedGrid = host.VisualChildren().First() as Grid; 
            }


             [Test]
             public void VisiblityCanBeSetBeforeAttaching()
             {
                 behaviour = new PopUpBehavior.PopUpBehaviour {PopUp = new UIElement(), Visible = true};
             }

             [Test]
             public void AddedGridShouldSpanAllHostRows()
             {
                 Assert.AreEqual( Grid.GetRowSpan(addedGrid), host.RowDefinitions.Count);
             }

             [Test]
             public void AddedGridShouldSpanAllHostColumns()
             {
                 Assert.AreEqual(Grid.GetColumnSpan(addedGrid), host.ColumnDefinitions.Count);
             }


             [Test]
             public void PopUpIsAddedToGrid()
             {
                 var popup = addedGrid.VisualChildren().First();
                 Assert.AreSame(popup, behaviour.PopUp);
             }

        }

         [TestFixture, RequiresSTA]
         public class PopUpBehaviourWhenClosedScenario
         {
             private Grid host;
             private PopUpBehavior.PopUpBehaviour behaviour;
             private Grid addedGrid;

             [SetUp]
             public void SetUp()
             {
                 behaviour = new PopUpBehavior.PopUpBehaviour { PopUp = new FrameworkElement(), Modal = true};
                 host = new Grid();
                 Interaction.GetBehaviors(host).Add(behaviour);
                 behaviour.Visible = true;
                 addedGrid = host.VisualChildren().First() as Grid;
                 ApplicationCommands.Close.Execute(null, behaviour.PopUp);
                 
             }



             [Test]
             public void VisibilityBindingsAreNotLost()
             {
                 var box = new CheckBox();
                 BindingOperations.SetBinding(behaviour, PopUpBehavior.PopUpBehaviour.VisibleProperty, new Binding("IsChecked") { Source = box, Mode = BindingMode.TwoWay });
                 box.IsChecked = true;
                 ApplicationCommands.Close.Execute(null, behaviour.PopUp);

                 Assert.IsFalse(box.IsChecked.Value);
                 Assert.IsFalse(behaviour.Visible);
             }


             [Test]
             public void GridIsRemovedFromHost()
             {
                 Assert.AreEqual(0, host.Children.Count);
             }

             [Test]
             public void PopUpIsDisconnected()
             {
                Assert.IsNull(((FrameworkElement)behaviour.PopUp).Parent);
                Assert.IsFalse(addedGrid.Children.Contains(behaviour.PopUp));
             }


             [Test]
             public void ModalBehaviourIsRemoved()
             {
                Assert.IsFalse(Interaction.GetBehaviors(addedGrid).Contains(behaviour.ModalBehaviour));
             }

         }


    }
}
